package com.gz;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;

public class homework1 {
    public static void main(String[] args) {
        ArrayList<Employee> arrayList = new ArrayList<>();
        arrayList.add(new Employee("gz",40000,new MyDate(2005,10,18)));
        arrayList.add(new Employee("gz",20000,new MyDate(2005,9,18)));
        arrayList.add(new Employee("liubei",30000,new MyDate(2004,7,19)));
        arrayList.sort(new Comparator<Employee>() {
            public int compare(Employee o1, Employee o2) {
                //若o1,o2不是Employee类型，则返回
                if(!(o1 instanceof Employee && o2 instanceof Employee))
                {
                    System.out.println("类型错误。。");
                    return 0;
                }

                if(o1.getName().compareTo(o2.getName()) == 0)
                    //这里年月日的比较应该放在MyDate()类中完成
                    /*if(o1.getBirthday().getYear()==o2.getBirthday().getYear()){
                        if(o1.getBirthday().getMonth()==o2.getBirthday().getMonth()){
                            return o2.getBirthday().getDay()-o1.getBirthday().getDay(); //年龄从小到大，也就是年份越大年龄越小
                        }
                    }
                return o2.getBirthday().getYear()-o1.getBirthday().getYear();*/
                    return o1.getBirthday().compareTo(o2.getBirthday());
                return o1.getName().compareTo(o2.getName());//字母从小到大
            }
        });
        System.out.println("排序 "+arrayList);

    }
}

class Employee{
    public Employee(String name, double salary, MyDate birthday) {
        this.name = name;
        this.salary = salary;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public double getSalary() {
        return salary;
    }

    public void setName(String name) {
        this.name = name;
    }

     private  String name;
    private double salary;
    private MyDate birthday;

    @Override
    public String toString() {
        return "\nEmployee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate implements Comparable<MyDate>{
    public MyDate( int year,int month, int day) {
        this.month = month;
        this.year = year;
        this.day = day;
    }

    private int year;
    private int month;
    private int day;

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {
        if(year==o.year){
            if(month==o.month){
                return o.day-day; //年龄从小到大，也就是年份越大年龄越小
            }
            return o.month-month;
        }
        return o.year-year;
    }
}